<template>
    <el-dialog :title="isEdit?'编辑坏箱':'新增坏箱'" :visible.sync="dialogForm" width="1100px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="135px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="箱号" prop="containerNo">
                        <auto-search-container-on-port v-model="form.containerNo" :name="form.containerNo" @matchRecord="handleSelect"></auto-search-container-on-port>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="尺寸">
                        <el-input v-model.trim="form.containerSize" :disabled="true"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="箱型">
                        <el-input v-model.trim="form.containerType" :disabled="true"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="空重">
                        <el-select v-model="form.ifHeavy" placeholder="请选择" :disabled="true">
                            <el-option v-for="[key, val] in _dictIfHeavyType" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="内外贸">
                        <el-select v-model="form.tradeType" placeholder="请选择" :disabled="true">
                            <el-option v-for="[key, val] in _dictTradeType" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="箱主">
                        <el-input v-model="form.ownerName" :disabled="true"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="箱损状态">
                        <auto-search-dic @select="v=>form.damageCode=v"
                                         @selectName="v=>form.damageName=v"
                                         :name="form.damageName"
                                         code="BAD_BOX"></auto-search-dic>
                    </el-form-item>
                </el-col>
                <el-col :span="16">
                    <el-form-item label="备注">
                        <el-input type="textarea" :rows="3" style="width: 188px" placeholder="请输入内容"
                                  :maxLength="$maxLength"
                                  v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addBadBox, updateBadBox } from "@/api/middleControl/middleControl";
    import { maxLength} from "@/utils/rules";
    import autoSearchContainerOnPort from '@/components/autocomplete/autoSearchContainerOnPort';
    import autoSearchDic from '@/components/autocomplete/autoSearchDic'

    export default {
        name: "roleForm",
        components: {autoSearchContainerOnPort,autoSearchDic },
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    containerNo:[{ required: true, message: '请输入箱号', trigger: 'change' }]
                },
                dialogForm: false,
                isEdit: false,
            }
        },
        methods: {
            clearForm() {
                return {
                    containerNo: '',
                    containerType: '',
                    containerSize: '',
                    ifHeavy: '',
                    tradeType: '',
                    ownerName: '',
                    ownerId: '',
                    remark: '',
                    damageName: '',
                    damageCode: '',
                }
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.isEdit ? this.handleToEdit() : this.handleToAdd()
                    } else {
                        this.$message({
                            message: '请检查表单',
                            type: 'error'
                        })
                    }
                })
            },
            // 新增
            handleToAdd() {
                addBadBox(this.form).then(res => {
                    this.$message({ message: '新增成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            // 编辑
            handleToEdit() {
                updateBadBox(this.form.id, this.form).then(res => {
                    this.$message({ message: '编辑成功!', type: 'success' })
                    this.dialogForm = false
                    this.$emit('load')
                })
            },
            handleSelect(item){
                let form=JSON.parse(JSON.stringify(item))
                this.form.containerNo= item.containerNo
                this.form.containerType= item.containerType
                this.form.containerSize= item.containerSize
                this.form.ifHeavy= item.ifHeavy
                this.form.tradeType= item.tradeType
                this.form.ownerName= item.ownerName
                this.form.ownerCode= item.ownerCode
            }
        },
        watch: {
            dialogForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields()
                    this.isEdit = false
                    this.form = this.clearForm()
                }
            },
        },
    }
</script>

<style scoped>

</style>
